package com.hxd.mall.module.usercenter.home

import android.annotation.SuppressLint
import android.text.SpannableString
import android.util.Log
import android.view.View
import com.hexin.locallibrary.banner.listener.OnBannerListener
import com.hxd.mall.R
import com.hxd.mall.base.BaseInjectFragment
import com.hxd.mall.constant.HxdConstants
import com.hxd.mall.constant.HxdConstants.IntentParamsKeyDefine.key_title_is_use
import com.hxd.mall.constant.HxdConstants.IntentParamsKeyDefine.key_title_name
import com.hxd.mall.constant.HxdConstants.IntentParamsKeyDefine.key_web_url
import com.hxd.mall.constant.HxdConstants.IntentParamsKeyDefine.person_info
import com.hxd.mall.constant.HxdConstantsRouterPath
import com.hxd.mall.constant.HxdConstantsRouterPath.Common.path_to_webview
import com.hxd.mall.constant.HxdConstantsRouterPath.PathUserCenter.path_user_center_bindorder_order
import com.hxd.mall.constant.HxdConstantsRouterPath.PathUserCenter.path_user_center_funs_manager
import com.hxd.mall.constant.HxdConstantsRouterPath.PathUserCenter.path_user_center_nick
import com.hxd.mall.constant.HxdConstantsRouterPath.PathUserCenter.path_user_center_order_query
import com.hxd.mall.constant.HxdConstantsRouterPath.PathUserCenter.path_user_center_setting
import com.hxd.mall.module.product.home.bean.HomeTypeBean
import com.hxd.mall.utils.local.*
import com.hxd.mall.utils.local.NavigationUtil.routerNavigation
import com.hxd.mall.utils.open.*
import kotlinx.android.synthetic.main.fragment_mine_layout.*
import kotlinx.android.synthetic.main.home_top_fg_layout.view.*

/**
 * @author zhaoyong
 * @description: 我的tab页
 * @date :2019-07-19 18:49
 */
class MineFragment : BaseInjectFragment<IMinePresenter>(), IMineView {

    private var mInfobean: PersonInfoBean? = null
    private var mOtherBean: HomeTypeBean? = null
    override fun attachLayoutRes(): Int = R.layout.fragment_mine_layout

    override fun initViews() {
        isReload = true

        addSetOnClickListener(
            settings, funs, order, invite, avator,
            question, inviteCodeCopy, weixinCopy, withdraw, tv_order_time
        ) {
            viewClick(it)
        }
    }

    override fun initData() {
        mPresenter?.getData(false)
        mPresenter?.getBannerData()
    }

    //刷新界面数据
    @SuppressLint("SetTextI18n")
    override fun showData(personInfoBean: PersonInfoBean?, isGoOn: Boolean) {
        if (null == personInfoBean) return
        mInfobean = personInfoBean
        personInfoBean?.data?.run {
            //昵称
            tv_nickName?.text = userProfile.nickname
            //头像
            avator?.loadCircle(userProfile.avatarUrl)
            //邀请码
            tv_code?.text = "邀请码：${userProfile.code}"
            //收益
            tv_income?.text = accountProfile.estimatedIncome
            //粉丝总数
            total_funs?.text = "粉丝：${userProfile.fansTotal}"

            //weixinCopy?.text = customerServiceProfile.phoneNo
            today.text = SpannableString("今日收益：¥${accountProfile.todayEstimatedIncome}").apply {
                StringUtil.setSpanClick(
                    this,
                    5,
                    "今日收益：¥${accountProfile.todayEstimatedIncome}".trim().length
                ) {
                }
            }
            now_month.text = SpannableString("本月预估：¥${accountProfile.monthEstimatedIncome}").apply {
                StringUtil.setSpanClick(
                    this,
                    5,
                    "本月预估：¥${accountProfile.monthEstimatedIncome}".trim().length
                ) {
                }
            }

        }
        if (isGoOn) {
            routerNavigation(
                path_user_center_funs_manager,
                person_info,
                GsonUtil.bean2json(mInfobean ?: "")
            )
        }
    }

    override fun showBannerData(t: HomeTypeBean?) {
        if (null == t) return
        mOtherBean = t
        showBanner()
    }

    /**
     * banner 数据
     */
    private fun showBanner() {
        if (mOtherBean?.data?.bannerLayout?.resources == null) {
            banner_Img.visibility = View.GONE
        } else
            mOtherBean?.data?.bannerLayout?.resources?.run {
                if (isEmpty()) {
                    banner_Img.visibility = View.GONE
                } else {
                    initBannerView(banner_Img, this) {}
                    banner_Img.visibility = View.VISIBLE
                }
            }
    }

    private fun viewClick(view: View) {
        when (view) {
            //设置
            settings -> {
                activity?.sendPoint(setting_button_click)
                routerNavigation(path_user_center_setting)
            }
            //粉丝
            funs -> {
                activity?.sendPoint(fans_management_button_click)
                if (null == mInfobean) {
                    mPresenter?.getData(true)
                    return
                }
                routerNavigation(
                    path_user_center_funs_manager,
                    person_info,
                    GsonUtil.bean2json(mInfobean ?: "")
                )
            }
            //订单
            order -> {
                activity?.sendPoint(order_management_button_click)
                routerNavigation(path_user_center_order_query)
            }
            //头像
            avator -> {
            }
            //邀请
            invite -> {
                activity?.sendPoint(invite_friends_button_click)
                routerNavigation(path_user_center_nick)
            }
            //常见问题
            question -> {
                activity?.sendPoint(a_button_click)
                mInfobean?.data?.questionProfile?.entryUri?.run {
                    routerNavigation(
                        path_to_webview,
                        mutableMapOf(
                            key_web_url to this,
                            key_title_name to "常见问题",
                            key_title_is_use to true
                        )
                    )
                }
            }
            //提现
            withdraw -> ToastUtil.showToast("本月收益在下月25日就可以提现啦")
            //复制按钮
            inviteCodeCopy -> {
                activity?.sendPoint(inviting_code_copy_button)
                setPrimaryClip(activity!!, mInfobean?.data?.userProfile?.code ?: "")
            }
            weixinCopy -> {
                // callPhone(mInfobean?.data?.customerServiceProfile?.wechatId ?: "",activity!!)
                setPrimaryClip(activity!!, mInfobean?.data?.customerServiceProfile?.wechatId ?: "")
            }
            // tv_order_time -> routerNavigation(path_user_center_bindorder_order)
        }
    }

}